package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.eduSchoolXwtj.EduSchoolXwtjKeyVO;
import com.jwsoft.manager.common.vo.eduSchoolXwtj.EduSchoolXwtjQueryVO;
import com.jwsoft.manager.common.vo.eduSchoolXwtj.EduSchoolXwtjVO;
import com.jwsoft.manager.core.dao.model.EduSchoolXwtj;
import com.jwsoft.manager.core.dao.service.EduSchoolXwtjService;
import com.jwsoft.manager.core.integration.EduSchoolXwtjIntegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
/**
 * 学位调剂业务实现类
 *
 * @author zhiyuan
 * @since 2023-03-09
 */
@ApiService(funcCode = "schoolXwtj", title = "学位调剂")
public class EduSchoolXwtjIntegrationImpl implements EduSchoolXwtjIntegration {

    @Autowired
    public EduSchoolXwtjService eduSchoolXwtjService;

    @Override
    @OpApi(funcCode = "schoolXwtj0001", title = "学位调剂分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduSchoolXwtjVO> getList(EduSchoolXwtjQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduSchoolXwtj> list = eduSchoolXwtjService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduSchoolXwtj> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduSchoolXwtjVO> resultList = Convert.toList(EduSchoolXwtjVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "schoolXwtj0002", title = "学位调剂根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduSchoolXwtjVO getById(EduSchoolXwtjKeyVO vo) {
        EduSchoolXwtj entity = eduSchoolXwtjService.getById(vo.getSchoolXwtjId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduSchoolXwtjVO result = Convert.convert(EduSchoolXwtjVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "schoolXwtj0003", title = "学位调剂保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduSchoolXwtjVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "schoolXwtj0004", title = "学位调剂新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "schoolXwtj0004",
            operationName = "学位调剂新增",
            dataType = "schoolXwtjId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSchoolXwtjVO add(EduSchoolXwtjVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduSchoolXwtj entity = Convert.convert(EduSchoolXwtj.class, vo);
        eduSchoolXwtjService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setSchoolXwtjId(entity.getSchoolXwtjId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "schoolXwtj0005", title = "学位调剂修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "schoolXwtj0005",
            operationName = "学位调剂修改",
            dataType = "schoolXwtjId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSchoolXwtjVO edit(EduSchoolXwtjVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolXwtjId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduSchoolXwtj oldEntity = eduSchoolXwtjService.getById(vo.getSchoolXwtjId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduSchoolXwtj entity = Convert.convert(EduSchoolXwtj.class, vo);
        eduSchoolXwtjService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "schoolXwtj0006", title = "学位调剂根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "schoolXwtj0006",
            operationName = "学位调剂删除",
            dataType = "schoolXwtjId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduSchoolXwtjKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolXwtjId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduSchoolXwtj entity = eduSchoolXwtjService.getById(vo.getSchoolXwtjId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduSchoolXwtjService.removeById(vo.getSchoolXwtjId());
    }
}
